﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq; 
using System.Web.UI;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_PayInPack : System.Web.UI.Page
    {
        #region[Declarable]

        public static string payType = "4";
        public static string strBgmYear;
        private Main main = new Main();
        private IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        private IBLL_HRM_MP_WorkAreas area = new B_HRM_MP_WorkAreas();
        private IBLL_FIM_MAS_BankAccounts acc = new B_FIM_MAS_BankAccounts();
        private IBLL_FIM_MAS_BankAccountTypes acctype = new B_FIM_MAS_BankAccountTypes();
        private IBLL_FIM_Payments p = new B_FIM_Payments();
        private IBLL_FIM_Payments_Payinpack pp = new B_FIM_Payments_Payinpack();
        private IBLL_HRM_PS_Employees emp = new B_HRM_PS_Employees();

        #endregion[Declarable]

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !string.IsNullOrEmpty(Request["do"]))
            {
                Export(Request["do"], Request["pip_ref_no"], Request["bgm_source_seq"], Request["work_area_seq"],
                    Request["bgm_year"]);
            }

            if (!IsPostBack)
            {
                int year = main.GetSystemControls().default_bgm_year.HasValue
                    ? main.GetSystemControls().default_bgm_year.Value
                    : DateTime.Now.Year;
                txtBgmYear.Text = year.ToString();
                txtBgmYear.MinValue = year - 10;
                txtBgmYear.MaxValue = year + 1;
                lblBgmYear.Text = year.ToString();

                UpdateUI1("ViewAll");
                LoadDDLFilterBgmSourceSeq();
                LoadDDLFilterWorkAreaSeq();
                LoadDDLBankAccSeq(ddlToBankAccSeq);
                //LoadRBLBankAccTypeSeq();

                ddlFilterBgmSourceSeq.SelectedIndex = 0;
                string strBgmSourceName = ddlFilterBgmSourceSeq.SelectedItem.Text;
                strBgmYear = txtBgmYear.Text;
                lblBgmSourceName.Text = strBgmSourceName;
                lblHeader.Text = "ข้อมูลรายการโอน Pay in pack";
                LoadRadGrid1();
                RadGrid1.DataBind();

                hdfDdlFilterBgmSourceSeq.Value = ddlFilterBgmSourceSeq.SelectedValue;
                hdfDdlFilterWorkAreaSeq.Value = ddlFilterWorkAreaSeq.SelectedValue;
                hdfTxtBgmYear.Value = txtBgmYear.Text;
            }
        }

        private void Export(string ado, string pip_ref_no, string bgm_source_seq, string work_area_seq, string bgm_year)
        {
            if (ado == "ExcelDetail")
            {
                LoadExcel3(pip_ref_no, bgm_source_seq, work_area_seq, bgm_year);
            }
            else if (ado == "ExportPayInPackData")
            {
                string errorString;
                PipExporter.LoadFixlenghtPip(pip_ref_no, out errorString);

                if (string.IsNullOrEmpty(errorString))
                {
                    Response.Write(errorString);
                    Response.End();
                }
            }
            else
            {
                throw new Exception("no method");
            }
        }

        protected void LoadRadGrid1()
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            RadGrid1.DataSource =
                pp.GetFull(strBgmSourceSeq, strBgmYear, strWorkAreaSeq)
                    .GroupBy(c => c.pip_ref_no)
                    .Select(g => g.First())
                    .ToList();
        }

        protected void LoadRadGrid2(string transfer_seq)
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            RadGrid2.DataSource =
                p.GetNotRealPayment(strBgmSourceSeq, strWorkAreaSeq, strBgmYear, payType)
                    .Where(m => m.record_status == "A")
                    .GroupBy(c => c.pay_seq)
                    .Select(g => g.First())
                    .ToList();
            RadGrid2.DataBind();
        }

        protected void LoadRadGrid3(string pip_ref_no)
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            RadGrid2.DataSource = p.GetRealPaid(pip_ref_no);
            RadGrid2.DataBind();
        }

        private void LoadExcel3(string pip_ref_no, string strBgmSourceSeq, string strWorkAreaSeq, string strBgmYearLocal)
        {
            var dtbs = new Dictionary<string, string>();
            //string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            //string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            var pip1 = pp.GetFull(strBgmSourceSeq, strBgmYearLocal, strWorkAreaSeq)
                .GroupBy(c => c.pip_ref_no).Select(g => g.First()).ToList()
                .FirstOrDefault(o => o.pip_ref_no == pip_ref_no);

            if (pip1 == null)
            {
                Response.Write("Pay-in-pack [" + pip_ref_no + "] Not found");
                Response.End();
                // main.MessageAlert(Page, "Pay-in-pack [" + pip_ref_no +"] Not found");
                return;
            }

            var acc1 = acc.GetList(null, null, null, null)
                .FirstOrDefault(o => o.bank_acc_seq == pip1.bank_acc_seq);
            var realPaids = p.GetRealPaid(pip_ref_no);
            var pay = realPaids.FirstOrDefault();

            dtbs.Add("F1", DateTime.Now.ToString() + "");
            dtbs.Add("B2", pip1.pip_no + "");
            dtbs.Add("E2", pip1.pip_ref_no + "");
            dtbs.Add("B3", pip1.pip_name + "");
            dtbs.Add("B4", pay == null || pay.pay_date == null ? "" : pay.pay_date.Value.ToString("d/M/yyyy"));
            dtbs.Add("B5", pip1.pip_date == null ? "" : pip1.pip_date.Value.ToString("d/M/yyyy"));
            dtbs.Add("B6", pip1.bank_acc_name);
            dtbs.Add("B7", acc1 == null ? "" : acc1.bank_acc_name + "");
            dtbs.Add("B8", pip1.contact_name + "");
            dtbs.Add("B9", (pip1.net_amount ?? 0).ToString("#,##0.00") + " บาท");
            dtbs.Add("B10", pip1.record_status_name + "");

            int row = 13;
            int rowSeq = 1;

            foreach (var realPaid in realPaids)
            {
                //เอาวันที่ออกเปลี่ยนเป็นเลข PQ
                //dtbs.Add("A" + row, !realPaid.pay_date.HasValue
                //    ? ""
                //    : realPaid.pay_date.Value.ToString("d/MM/yyyy"));
                dtbs.Add("A" + row, rowSeq.ToString() + "");
                dtbs.Add("B" + row, realPaid.ref_no_money_request + "");
                dtbs.Add("C" + row, realPaid.ref_no + "");
                dtbs.Add("D" + row, realPaid.pay_name + "");
                // เอาชื่อออกเปลี่ยนเป็นเลข RQ
                //dtbs.Add("C" + row, realPaid.contact_name + "");
                dtbs.Add("E" + row, realPaid.to_bank_acc_name + "");
                dtbs.Add("F" + row, !realPaid.amount.HasValue
                    ? ""
                    : realPaid.amount.Value.ToString("#,##0.00"));

                row++;
                rowSeq++;
            }

            new MailMerge().MergeExcel(
                Response,
                Server.MapPath("~/Reports/Templates/PayInPackDetailsView.xlsx"),
                string.Format("PayInPackDetail_{0}", DateTime.Now.ToString("yyyy_MM_dd_HHmmssffff")),
                null,
                dtbs, null, null);


        }

        protected void CheckPaid(IList<M_FIM_Payments> lstpaid)
        {
            //p.GetInfo();
            //pplst = new List<int>();
            //sustrlst = new List<int>();            
            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                RadGrid2.Items[i].Selected = true;
                //pplst.Add(Convert.ToInt32(RadGrid2.Items[i]["pay_seq"].Text));

                if (lstpaid.Count > 0)
                {
                    var ret =
                        lstpaid.ToList().Find(p => p.pay_seq.Equals(Convert.ToInt32(RadGrid2.Items[i]["pay_seq"].Text)));
                    if (ret == null)
                    {
                        RadGrid2.Items[i].Selected = false;
                        //sustrlst.Add(Convert.ToInt32(RadGrid2.Items[i]["pay_seq"].Text));
                    }
                }
            }
        }

        #region[header data]

        protected void LoadDDLFilterBgmSourceSeq()
        {
            ddlFilterBgmSourceSeq.DataSource = source.GetFull(true);
            ddlFilterBgmSourceSeq.DataBind();
        }

        protected void LoadDDLFilterWorkAreaSeq()
        {
            ddlFilterWorkAreaSeq.DataSource = area.GetList();
            ddlFilterWorkAreaSeq.DataBind();
        }

        protected void LoadDDLBankAccSeq(RadComboBox item)
        {
            item.DataSource = acc.GetList(null, ddlFilterBgmSourceSeq.SelectedValue, null, null);
            item.DataBind();
        }

        protected void ddlFilterBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
            LoadDDLBankAccSeq(ddlToBankAccSeq);

            string strBgmSourceName = ddlFilterBgmSourceSeq.SelectedItem.Text;
            lblBgmSourceName.Text = strBgmSourceName;

            hdfDdlFilterBgmSourceSeq.Value = ddlFilterBgmSourceSeq.SelectedValue;
        }

        protected void ddlFilterWorkAreaSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();

            hdfDdlFilterWorkAreaSeq.Value = ddlFilterWorkAreaSeq.SelectedValue;
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            strBgmYear = txtBgmYear.Text;
            lblBgmYear.Text = strBgmYear;
            LoadRadGrid1();
            RadGrid1.DataBind();

            hdfTxtBgmYear.Value = txtBgmYear.Text;
        }

        #endregion[header data]

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    PanelEdit2.Visible = false;
                    RadGrid2.Enabled = false;
                    lblAmountEdit.Visible = true;
                    lblAmount.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    PanelEdit2.Visible = false;
                    RadGrid2.Enabled = true;
                    lblAmountEdit.Visible = false;
                    lblAmount.Visible = true;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;
                    lblAmountEdit.Visible = true;
                    lblAmount.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    PanelEdit2.Visible = false;
                    RadGrid2.Enabled = false;
                    break;
            }
        }

        protected void ClearControl1()
        {
            txtTransferName.Text = string.Empty;
            lblAmount.Text = "0";
            lblAmountEdit.Text = "0.00";
            txtOtherAmount.Text = "0";
            lblTempPipRefNo.Text = string.Empty;
        }

        //============================ control events ==============================

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void RadGridRequest_ItemCommand(object source, GridCommandEventArgs e)
        { 
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadRadGrid1();
            } 
        }
        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" || e.CommandName == "Filter" || e.CommandName == "Page")
            {
                string strTransferSeq = null;
                if (RadGrid1.SelectedItems.Count > 0)
                {
                    try
                    {
                        strTransferSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["method_seq"].Text;
                    }
                    catch
                    {
                    }
                }

                LoadRadGrid2(strTransferSeq);
            }
            else if (e.CommandName == "RowClick")
            {
                if (e.Item.OwnerTableView.ParentItem == null)
                {
                    double dSum = 0;
                    for (int i = 0; i < RadGrid2.Items.Count; i++)
                    {
                        if (RadGrid2.Items[i].Selected)
                        {
                            ////disable each odd item check box
                            //if (e.Item.ItemType == GridItemType.Item)
                            //{
                            //    GridDataItem item = (GridDataItem)e.Item;
                            //    CheckBox checkBox = (CheckBox)item["ClientSelectColumn"].Controls[0];
                            //    checkBox.Enabled = false;
                            //}

                            dSum = dSum + Convert.ToDouble(main.WebString(RadGrid2.Items[i]["amount"].Text));
                        }
                    }

                    lblAmount.Text = dSum.ToString();
                }
            }
            else if (e.CommandName == "Export")
            {
                RadGrid2.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.Biff;
                RadGrid2.MasterTableView.ExportToExcel();
            }
        }


        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
            LoadRadGrid2(null);
            txtRefNo.Text = main.GenNewRefNo("fim_payments_payinpack", "PI", null, null);
            dpTransferDate.SetValue(null);
            lblEmpSeq.Text = new SsMng().EmpSeq;
            lblEmpName.Text = SsMngCurrentLoginEmployeeName;
            lblAmount.Text = "0";
            ClearControl1();
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strUserID = new SsMng().EmpSeq; //Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strPipRefNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pip_ref_no"].Text;
                lblTempPipRefNo.Text = strPipRefNo;
                lblEmpSeq.Text = strUserID;
                lblEmpName.Text = SsMngCurrentLoginEmployeeName;
                string strTransferName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pip_name"].Text;
                string strRefNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pip_no"].Text;
                string strTransferDate = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pip_date"].Text;
                string strToBankAccSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bank_acc_seq"].Text;
                string strAmount = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["net_amount"].Text;
                string strOtherAmount4 = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["fee_amount"].Text;

                LoadRadGrid3(strPipRefNo);

                IList<M_FIM_Payments> lstpaid = p.GetRealPaid(strPipRefNo);
                CheckPaid(lstpaid);
                ////RadGrid2.Enabled = false;
                double dSum = 0;
                for (int i = 0; i < RadGrid2.Items.Count; i++)
                {
                    ////RadGrid2.Enabled = false;
                    dSum = dSum + Convert.ToDouble(main.WebString(RadGrid2.Items[i]["amount"].Text));
                    //    if (RadGrid2.Items[i].Selected)
                    //    {                        
                    //        RadGrid2.Items[i].OwnerTableView.Enabled = false;
                    //        dSum = dSum + Convert.ToDouble(main.WebString(RadGrid2.Items[i]["amount"].Text));
                    //    }
                }

                strAmount = (main.WebString(strAmount).Length > 0 ? main.WebString(strAmount) : "0");
                lblAmountEdit.Text = dSum.ToString("#,##0.00");
                txtOtherAmount.Text = strOtherAmount4; //(Convert.ToDouble(strAmount) - dSum).ToString();
                txtTransferName.Text = main.WebString(strTransferName);
                txtRefNo.Text = main.WebString(strRefNo);
                dpTransferDate.SetValue(main.WebString(strTransferDate));
                ddlToBankAccSeq.SelectedValue = main.WebString(strToBankAccSeq);
                //ddlToBankAccSeq_SelectedIndexChanged(null, null);

            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                M_FIM_Payments_Payinpack info = new M_FIM_Payments_Payinpack();
                string strUserID = new SsMng().EmpSeq;
                DateTime dtNow = DateTime.Now;
                string strPipRefNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pip_ref_no"].Text;
                info.bgm_source_seq = Convert.ToInt32(ddlFilterBgmSourceSeq.SelectedValue);
                info.work_area_seq = Convert.ToInt32(ddlFilterWorkAreaSeq.SelectedValue);
                if (String.IsNullOrEmpty(txtBgmYear.Text)) txtBgmYear.Text = DateTime.Now.Year.ToString("0000");
                info.bgm_year = Convert.ToInt32(txtBgmYear.Text);
                info.pip_ref_no = strPipRefNo;
                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = main.ConvertNullDateTime(dtNow);

                pp.Delete(info);
                LoadRadGrid1();
                RadGrid1.DataBind();
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            string strMethodSeq = payType; //rblTransferType.SelectedValue;
            string strTransferName = null;
            string strRefNo = null;
            string strTransferDate = null;
            string strToBankAccSeq = null;
            string strBankAccTypeSeq = null;
            string strAmount = null;
            string strEmpSeq = null;
            string strUserID = new SsMng().EmpSeq;
            DateTime dtNow = DateTime.Now;
            if (String.IsNullOrEmpty(strBgmSourceSeq)) strBgmSourceSeq = "1";
            if (String.IsNullOrEmpty(strWorkAreaSeq)) strWorkAreaSeq = "1";
            if (String.IsNullOrEmpty(txtBgmYear.Text)) txtBgmYear.Text = DateTime.Now.Year.ToString("0000");
            if (String.IsNullOrEmpty(strMethodSeq)) strMethodSeq = "4";
            //string strBgmYear = txtBgmYear.Text;
            strTransferName = txtTransferName.Text;
            strRefNo = txtRefNo.Text;
            strTransferDate = dpTransferDate.GetStringValue();
            strToBankAccSeq = ddlToBankAccSeq.SelectedValue;

            if (rblBankAccTypeSeq.Visible)
                strBankAccTypeSeq = rblBankAccTypeSeq.SelectedValue;

            strAmount = (lblAmount.Text.Length > 0 ? lblAmount.Text : "0");
            string strOtherAmount4 = (txtOtherAmount.Text.Length > 0 ? txtOtherAmount.Text : "0");

            strAmount = (main.ConvertNullDouble(strAmount) + main.ConvertNullDouble(strOtherAmount4)).ToString();
            strEmpSeq = lblEmpSeq.Text;

            IList<M_FIM_Payments_Payinpack> lst = new List<M_FIM_Payments_Payinpack>();
            string pip_ref_no = DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00") +
                                DateTime.Now.Day.ToString("00")
                                + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") +
                                DateTime.Now.Second.ToString("00") + DateTime.Now.Millisecond.ToString("000");
            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                if (RadGrid2.Items[i].Selected)
                {
                    M_FIM_Payments_Payinpack info = new M_FIM_Payments_Payinpack();
                    ////info.transfer_seq = Convert.ToInt32(strTransferSeq);
                    info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
                    info.work_area_seq = Convert.ToInt32(strWorkAreaSeq);
                    info.bgm_year = Convert.ToInt32(strBgmYear);
                    info.pip_no = strRefNo;
                    info.pip_name = strTransferName;
                    info.pip_date = main.ConvertNullDateTime(strTransferDate);
                    //info.from_bank_acc_seq = main.ConvertNullInt(strFromBankAccSeq);
                    info.bank_acc_seq = main.ConvertNullInt(strToBankAccSeq);
                    info.fee_amount = main.ConvertNullDouble(strOtherAmount4);
                    info.net_amount = main.ConvertNullDouble(strAmount);
                    //info.emp_seq = main.ConvertNullInt(strEmpSeq);
                    info.record_status = "W";
                    info.created_by = main.ConvertNullInt(strUserID);
                    info.created_when = main.ConvertNullDateTime(dtNow);
                    info.pip_ref_no = pip_ref_no;
                    info.contact_seq = main.ConvertNullInt(RadGrid2.Items[i]["contact_seq"].Text);
                    //Convert.ToInt32(RadGrid2.Items[i]["contact_seq"].Text);//
                    info.to_bank_acc_id = main.ConvertNullInt(RadGrid2.Items[i]["to_bank_acc_id"].Text);
                    info.pay_seq = Convert.ToInt32(RadGrid2.Items[i]["pay_seq"].Text);
                    //info.pip_id = i + 1;
                    lst.Add(info);
                }
            }

            if (lst.Count > 0)
            {
                bool ret = pp.InsertList(lst);
                if (ret)
                {
                    LoadRadGrid1();
                    RadGrid1.DataBind();
                    UpdateUI1("Insert");
                    ClearControl1();
                }
                else
                {
                    main.MessageAlert(Page, "พบข้อผิดพลาด! ไม่สามารถเพิ่มข้อมูลได้");
                }

            }
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            try
            {
                string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
                string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
                string strTransferName = null;
                string strRefNo = null;
                string strTransferDate = null;
                string strToBankAccSeq = null;
                //string strBankAccTypeSeq = null;
                string strAmount = null;
                string strUserID = new SsMng().EmpSeq;
                DateTime dtNow = DateTime.Now;

                strTransferName = txtTransferName.Text;
                strRefNo = txtRefNo.Text;
                strTransferDate = dpTransferDate.GetStringValue();
                strToBankAccSeq = ddlToBankAccSeq.SelectedValue;
                strAmount = (lblAmountEdit.Text.Length > 0 ? lblAmountEdit.Text : "0");
                string strOtherAmount4 = (txtOtherAmount.Text.Length > 0 ? txtOtherAmount.Text : "0");
                strAmount = (main.ConvertNullDouble(strAmount) + main.ConvertNullDouble(strOtherAmount4)).ToString();

                if (String.IsNullOrEmpty(strBgmYear)) strBgmYear = DateTime.Now.Year.ToString("0000");
                List<M_FIM_Payments_Payinpack> ll = new List<M_FIM_Payments_Payinpack>();
                M_FIM_Payments_Payinpack info = new M_FIM_Payments_Payinpack();
                info.pip_no = strRefNo;
                info.pip_name = strTransferName;
                info.pip_date = main.ConvertNullDateTime(strTransferDate);
                info.bank_acc_seq = main.ConvertNullInt(strToBankAccSeq);
                info.fee_amount = main.ConvertNullDouble(strOtherAmount4);
                info.net_amount = main.ConvertNullDouble(strAmount);
                info.updated_by = Convert.ToInt32(strUserID);
                info.updated_when = dtNow;
                info.pip_ref_no = lblTempPipRefNo.Text;
                info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
                info.work_area_seq = Convert.ToInt32(strWorkAreaSeq);
                info.bgm_year = Convert.ToInt32(strBgmYear);
                pp.Update(info);
                //    List<M_FIM_Payments_Payinpack> l = new List<M_FIM_Payments_Payinpack>();
                //    List<M_FIM_Payments_Payinpack> ll = new List<M_FIM_Payments_Payinpack>();
                //for (int i = 0; i < RadGrid2.Items.Count; i++)
                //{                    
                //    if (RadGrid2.Items[i].Selected)
                //    {
                //        M_FIM_Payments_Payinpack info = new M_FIM_Payments_Payinpack();
                //        info.pip_no = strRefNo;
                //        info.pip_name = strTransferName;
                //        info.pip_date = main.ConvertNullDateTime(strTransferDate);
                //        info.bank_acc_seq = main.ConvertNullInt(strToBankAccSeq);
                //        info.fee_amount = main.ConvertNullDouble(strOtherAmount4);
                //        info.net_amount = main.ConvertNullDouble(strAmount);
                //        info.updated_by = Convert.ToInt32(strUserID);
                //        info.updated_when = dtNow;
                //        info.pip_ref_no = lblTempPipRefNo.Text;
                //        info.pay_seq = Convert.ToInt32(RadGrid2.Items[i]["pay_seq"].Text);
                //        if (!String.IsNullOrEmpty(RadGrid2.Items[i]["contact_seq"].Text))
                //            info.contact_seq = Convert.ToInt32(RadGrid2.Items[i]["contact_seq"].Text);
                //        else
                //            info.contact_seq = null;
                //        info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
                //        info.work_area_seq = Convert.ToInt32(strWorkAreaSeq);
                //        info.pip_id = i + 1;
                //        info.bgm_year = Convert.ToInt32(strBgmYear);
                //        l.Add(info);
                //    }
                //    else {
                //        M_FIM_Payments_Payinpack info = new M_FIM_Payments_Payinpack();
                //        info.pip_no = strRefNo;
                //        info.pip_name = strTransferName;
                //        info.pip_date = main.ConvertNullDateTime(strTransferDate);
                //        info.bank_acc_seq = main.ConvertNullInt(strToBankAccSeq);
                //        info.fee_amount = main.ConvertNullDouble(strOtherAmount4);
                //        info.net_amount = main.ConvertNullDouble(strAmount);
                //        info.updated_by = Convert.ToInt32(strUserID);
                //        info.updated_when = dtNow;
                //        info.pip_ref_no = lblTempPipRefNo.Text;
                //        info.pay_seq = Convert.ToInt32(RadGrid2.Items[i]["pay_seq"].Text);
                //        if (!String.IsNullOrEmpty(RadGrid2.Items[i]["contact_seq"].Text))
                //            info.contact_seq = Convert.ToInt32(RadGrid2.Items[i]["contact_seq"].Text);
                //        else
                //            info.contact_seq = null;
                //        info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
                //        info.work_area_seq = Convert.ToInt32(strWorkAreaSeq);
                //        info.bgm_year = Convert.ToInt32(strBgmYear);
                //        info.pip_id = i + 1;
                //        ll.Add(info);
                //    }
                //}
                //if (l.Count > 0) {
                //    pp.UpdateList(l);
                //}
                //if (ll.Count > 0) {
                //    foreach (M_FIM_Payments_Payinpack i in ll)
                //    {
                //        pp.Delete2(i);
                //    }
                //}

                LoadRadGrid1();
                RadGrid1.DataBind();
                UpdateUI1("Update");
                ClearControl1();
            }
            catch (Exception ex)
            {
                main.MessageAlert(Page, "พบข้อผิดพลาด! ไม่สามารถแก้ไขข้อมูลได้");
            }

        }

        private string SsMngCurrentLoginEmployeeName
        {
            get
            {
                var info = emp.GetInfo(new SsMng().EmpSeq);
                return info == null ? "" : info.full_name;
            }
        }

        protected void btnCheckAll_Click(object sender, EventArgs e)
        {
            CheckAll();
        }

        protected void btnUnCheckAll_Click(object sender, EventArgs e)
        {
            CheckAll(true);
        }

        private void CheckAll(bool uncheck = false)
        {
            //foreach (GridDataItem di in RadGrid2.Items)
            //{
            //    di[""]
            //}
        }
    }
}